package com.zxjbyte.yiyi.module.plugin.socket.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.Map;

/**
 * 自定义握手拦截器
 * @Author zhangxingjia
 * @Date 2024/7/14 21:19
 * @Version: 1.0
 */
@Slf4j
public class YiyiHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        log.info("YiyiHandshakeInterceptor beforeHandshake, URI: {}, RemoteAddress: {}", request.getURI(), request.getRemoteAddress());
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, @Nullable Exception ex) {
        log.info("YiyiHandshakeInterceptor afterHandshake completed");
        super.afterHandshake(request, response, wsHandler, ex);
    }
}
